package org.java.mathgame.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import org.java.mathgame.MathGame;

import java.util.ArrayList;


public class HighScoreScreen implements Screen {
    private MathGame game;
    private SpriteBatch spriteBatch;
    private BitmapFont font;
    private Stage stage;
    // constructor to keep a reference to the main Game class
    public HighScoreScreen(final MathGame game){
        this.game = game;
        spriteBatch = new SpriteBatch();
        font = new BitmapFont();
        stage = new Stage();

        TextButton.TextButtonStyle infoStyle = new TextButton.TextButtonStyle();
        infoStyle.up = new ButtonBackground(new Color(35/255f,35/255f,35/255f,1));
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/MyriadPro.ttf"));
        infoStyle.font = generator.generateFont((int) (Gdx.graphics.getHeight()/30f));

        Label.LabelStyle labelStyle = new Label.LabelStyle();
        labelStyle.font = generator.generateFont((int) (Gdx.graphics.getHeight()/25f));
        labelStyle.background = new ButtonBackground(new Color(0,0,0,0));

        Label.LabelStyle backStyle = new Label.LabelStyle();
        backStyle.font = generator.generateFont((int) (Gdx.graphics.getHeight()/30f));
        backStyle.background = new ButtonBackground(new Color(0,0,0,0));

        Label backLabel = new Label("  <",backStyle);


        TextButton backButton = new TextButton("High Scores ",infoStyle);

        backButton.getLabel().remove();
        backButton.clear();
        backButton.add(backLabel).expandX().left();
        backButton.add(backButton.getLabel());
        backButton.addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                game.setScreen(game.mainMenuScreen);
            }
        });
        TextButton tChallenge = new TextButton("Time Challenge",infoStyle);
        TextButton fClearing = new TextButton("Field Clearing",infoStyle);
        tChallenge.pad(5);
        fClearing.pad(5);
        tChallenge.clear();
        tChallenge.add(tChallenge.getLabel()).expandX().left();
        fClearing.clear();
        fClearing.add(fClearing.getLabel()).expandX().left();
        Table rootTable = new Table();
        rootTable.setFillParent(true);
        rootTable.setBackground(new ButtonBackground(new Color(238 / 255f, 238 / 255f, 238 / 255f, 1)));
        rootTable.pad(10);
        Table topPanel = new Table();
        Table bottomPanel = new Table();

        //ArrayList<Table> tChallengeScores;
        //ArrayList<Table> fClearingScores;

        Color[] colors = new Color[]{
                new Color(210 / 255f, 57 / 255f, 59 / 255f, 1),
                new Color(58 / 255f, 143 / 255f, 208 / 255f, 1),
                new Color(59 / 255f, 209 / 255f, 112 / 255f, 1),
                new Color(208 / 255f, 147 / 255f, 58 / 255f, 1)};


        topPanel.setBackground(new ButtonBackground(new Color(203/255f,203/255f,203/255f,1)));
        topPanel.pad(5);
        topPanel.add(tChallenge).expandX().left().width(Gdx.graphics.getWidth()/2f);
        topPanel.row();
        for(int i = 0; i<4; i++){
            Table scoreRow = new Table();
            scoreRow.setBackground(new ButtonBackground(colors[i]));
            /*Label leftLabel = new Label(Integer.toString(i+1)+".....",labelStyle);
            Label rightLabel = new Label(Integer.toString(i+1)+".....",labelStyle);*/
            Label leftLabel = new Label(Integer.toString(i+1) + ". " + game.getHighScoreHandler().getHighScore(1)[i].getName(),labelStyle);
            Label rightLabel = new Label(Integer.toString(game.getHighScoreHandler().getHighScore(1)[i].getScore()),labelStyle);
            topPanel.add(scoreRow).fillX().expandX().space(5);
            scoreRow.add(leftLabel).expand().left();
            scoreRow.add(rightLabel).expand().right();
            scoreRow.pad(5);
            topPanel.row();
            scoreRow.debug();
        }

        bottomPanel.setBackground(new ButtonBackground(new Color(203/255f,203/255f,203/255f,1)));
        bottomPanel.pad(5);
        bottomPanel.add(fClearing).expandX().left().width(Gdx.graphics.getWidth() / 2f);
        bottomPanel.row();
        for(int i = 0; i<4; i++){
            Table scoreRow = new Table();
            scoreRow.setBackground(new ButtonBackground(colors[i]));
            //Label leftLabel = new Label(Integer.toString(i+1)+".....",labelStyle);
            Label leftLabel = new Label(Integer.toString(i+1) + ". " + game.getHighScoreHandler().getHighScore(2)[i].getName(),labelStyle);
            Label rightLabel = new Label(Integer.toString(game.getHighScoreHandler().getHighScore(2)[i].getScore()),labelStyle);
            bottomPanel.add(scoreRow).fillX().expandX().space(5);
            scoreRow.add(leftLabel).expand().left();
            scoreRow.add(rightLabel).expand().right();
            scoreRow.pad(5);
            bottomPanel.row();
            scoreRow.debug();
        }

        rootTable.add(backButton).fillX().expandY().top();
        rootTable.row();
        rootTable.add(topPanel).expandX().fillX().space(5).expandY();
        rootTable.row();
        rootTable.add(bottomPanel).expandX().fillX().space(5).expandY();
        stage.addActor(rootTable);
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0.5f, 0.3f, 0.6f, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        font.setColor(1f, 1f, 1f, 1f);
        //if (Gdx.input.justTouched()) // use your own criterion here
        //    game.setScreen(game.mainMenuScreen);
        stage.draw();
        //Table.drawDebug(stage);
    }


    @Override
    public void resize(int width, int height) {
    }


    @Override
    public void show() {
        // called when this screen is set as the screen with game.setScreen();
        Gdx.input.setInputProcessor(stage);
    }


    @Override
    public void hide() {
        // called when current screen changes from this to a different screen
        Gdx.input.setInputProcessor(null);
    }


    @Override
    public void pause() {
    }


    @Override
    public void resume() {
    }


    @Override
    public void dispose() {
        // never called automatically
    }
}